#include "PLTimer.h"
#include <Winbase.h>

/**
  *	Constructs a new PLTimer object. Queries the frequency of the performance
  * counter and sets the number of counts at the construction time.
  */
PLTimer::PLTimer( void )
{
	QueryPerformanceFrequency(&mFrequency);
	QueryPerformanceCounter(&mBeginCounts);
	mCounts = mBeginCounts;
}

/**
  *	Returns the number of seconds since the timer object was constructed.
  *
  * @return number of seconds since the timer object was constructed
  */
double PLTimer::GetSecondsSinceConstruction( void )
{
	LARGE_INTEGER helper;
	QueryPerformanceCounter(&helper);

	return (static_cast<double>( helper.QuadPart ) - mBeginCounts.QuadPart) /
		mFrequency.QuadPart;
}

/**
  *	Stores the current number of counts of the performance counter when it is
  * called and returns the time in seconds since the last call of Toc(). When
  * Toc is called the first time it returns the time since construction of the
  * timer object.
  *
  * @return time in seconds since the last call of Toc()
  */
double PLTimer::Toc( void )
{
	LARGE_INTEGER helper;
	double time;

	QueryPerformanceCounter(&helper);

	time = (static_cast<double>( helper.QuadPart ) - mCounts.QuadPart) /
		mFrequency.QuadPart;
	mCounts = helper;

	return time;
}